package com.wxmsg;

import com.common.Constant;
import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxMessageHandler;
import com.soecode.wxtools.bean.WxXmlMessage;
import com.soecode.wxtools.bean.WxXmlOutMessage;
import com.soecode.wxtools.exception.WxErrorException;
import com.stock.StockMsg;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.Map;

/**
 * Created by lzb on 2018/5/11.
 */
public class EventHandle implements WxMessageHandler {

    private RedisTemplate redisTemplate = (RedisTemplate) Constant.ctx.getBean("redisTemplate");


    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, IService iService) throws WxErrorException {
//        if(redisTemplate==null)redisTemplate= Constant.ctx.getBean(RedisTemplate.class);
        WxXmlOutMessage xmlOutMsg = null;

        switch (wxMessage.getEvent()){
            case "subscribe":
                xmlOutMsg = WxXmlOutMessage.TEXT().content("欢迎关注!!!").toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
                break;

            case "CLICK":
                String msg="不支持!";
                switch (wxMessage.getEventKey()){
                    case "nowPrice":

                        msg= StockMsg.getPrice(Arrays.asList(
                                (String[]) redisTemplate.opsForSet().members(wxMessage.getFromUserName()).toArray(new  String[]{})
                        ),
                                Arrays.asList((String[]) redisTemplate.opsForSet().members(wxMessage.getFromUserName()+"_BUY").toArray(new  String[]{}))
                                );
                        break;

                    case "MDGE"://美的与格力的价格差以及建议
                        msg=StockMsg.getMDGE();

                        break;
                }

                xmlOutMsg = WxXmlOutMessage.TEXT().content(msg).toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
                break;

            default:
                xmlOutMsg = WxXmlOutMessage.TEXT().content("不支持事件!!!").toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
                break;
        }
        return xmlOutMsg;
    }
}
